package chapter8;

import java.util.Collection;
import java.util.HashSet;
import java.util.function.Predicate;

/**
 * 使用Predicate接口对集合元素做过滤统计
 */
public class PredicateTest2 {
    public static void main(String[] args) {
        Collection books = new HashSet();
        books.add("轻量级Java EE企业应用实战");
        books.add("疯狂Android讲义");
        books.add("疯狂IOS讲义");
        books.add("疯狂Ajax讲义");
        books.add("疯狂Java讲义");
        System.out.println("集合中包含疯狂的书籍有" + calAll(books, (ele)-> ((String)ele).contains("疯狂")));
        System.out.println("集合中包含Java的书籍有:" + calAll(books, (ele)->((String)ele).contains("Java")));
        System.out.println("集合中书名长度大于10的有:" + calAll(books, (ele)->((String)ele).length() > 10));
    }

    private static int calAll(Collection books, Predicate predicate) {
        int total = 0;
        for(Object book : books) {
            if(predicate.test(book)) {
                total++;
            }
        }
        return total;
    }
}
